package com.avocado.bt;

import com.avocado.bt.anno.NodeAlias;
import com.avocado.util.ClassUtil;

import java.lang.reflect.Modifier;
import java.util.Set;

/**
 * @Author: zsh
 * @Date: 2023/10/18 12:00
 */
public class BTNodeScan {

    public static void scan() throws NoSuchMethodException {
        Set<Class<IBTNode>> nodes = ClassUtil.getSubClasses("com.avocado.bt", IBTNode.class);
        for (Class<IBTNode> node : nodes) {
            if (Modifier.isInterface(node.getModifiers()) || Modifier.isAbstract(node.getModifiers())) {
                continue;
            }
            // 要有这个构造方法，没有就报错，算是check
            node.getConstructor(String.class);
            String name = node.getSimpleName();
            NodeAlias annotation = node.getAnnotation(NodeAlias.class);
            if (annotation != null) {
                name = annotation.value();
            }
            if (BTManager.contain(name)) {
                throw new RuntimeException("节点名字重复" + name);
            }
            BTManager.register(name, node);
        }
    }


}
